package com.wangzhenyu.learn.test.android_.fragmentdoc

import android.widget.Toast
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.wangzhenyu.learn.test.android_.fragmentViewModel.FirstFragment

//tools:layout="@layout/test_fragment_first",可以在预览视图中看到fragment预览界面
//静态创建
//  创建FragmentContainerView布局，详情见MainActivity
//
//
//
//
//
//
//
//
//
//
//
//


//通过adb shell dumpsys activity com.wangzhenyu.learn查看已有Fragment
//旋转屏幕会重新调用onCreate(),这时要注意避免重新创建Fragment
//addToBackStack:将Fragment添加到容器的同时也添加到BackStack里
@Suppress("UNUSED")
object InstanceOfFragment {

    //添加一个Fragment(通过add)
    fun test01(activity: AppCompatActivity, @IdRes fragmentId: Int) {
        val transaction = activity.supportFragmentManager.beginTransaction()
        transaction.add(fragmentId, FirstFragment::class.java, null).setReorderingAllowed(true)
            .commit()
    }

    //添加Fragment(带Tag)
    fun test02(activity: AppCompatActivity, @IdRes fragmentId: Int) {
        val transaction = activity.supportFragmentManager.beginTransaction()
        transaction.add(fragmentId, FirstFragment::class.java, null, "TestFragment")
            .setReorderingAllowed(true)
            .commit()
    }

    //根据id/tag查找fragment，返回找到的最上面的一个（如果没有，则从backStack里找，并返回最先添加的一个，如果没有BackStack，则返回null）
    //通过ID查找(find)Fragment
    fun test03(activity: AppCompatActivity, @IdRes fragmentId: Int): Fragment? {
        val fragmentManager = activity.supportFragmentManager
        val fragment = fragmentManager.findFragmentById(fragmentId)
        if (fragment != null) Toast.makeText(activity, fragment.toString(), Toast.LENGTH_SHORT)
            .show()
        return fragment
    }

    //通过Tag查找(find)Fragment
    fun test04(activity: AppCompatActivity, @IdRes fragmentId: Int): Fragment? {
        val fragmentManager = activity.supportFragmentManager
        val fragment = fragmentManager.findFragmentByTag("TestFragment")
        if (fragment != null) Toast.makeText(activity, fragment.toString(), Toast.LENGTH_SHORT)
            .show()
        return fragment
    }

    //移除(remove)Fragment(移除时不会移除backStack中的Fragment)
    fun test05(activity: AppCompatActivity, @IdRes fragmentId: Int, fragment: Fragment) {
        val fragmentManager = activity.supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        transaction.remove(fragment).commit()
    }

    //不带addToBackStack
    //将当前容器上的Fragment全部移除，添加新的Fragment
    fun test06(activity: AppCompatActivity, @IdRes fragmentId: Int) {
        val transaction = activity.supportFragmentManager.beginTransaction()
        transaction.replace(fragmentId, FirstFragment::class.java, null).commit()
    }

    //带addToBackStack
    //将当前容器上的Fragment全部移除，添加新的Fragment
    //BackStack里fragment仍然存在 按返回键依然会响应到BackStack，也就是弹出BackStack里的Fragment
    fun test07(activity: AppCompatActivity, @IdRes fragmentId: Int) {
        val transaction = activity.supportFragmentManager.beginTransaction()
        transaction.replace(fragmentId, FirstFragment::class.java, null)
            .addToBackStack("Test").commit()
    }


    //show/hide 仅隐藏View，什么都不销毁
    //attach/detach 仅销毁View
    //add/remove 销毁View和整个Fragment

}